package org.bsplus.client.util;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.ServiceDefTarget;
import org.bsplus.rpc.IssueRPCService;
import org.bsplus.rpc.IssueRPCServiceAsync;
import org.bsplus.rpc.ProjectRPCService;
import org.bsplus.rpc.ProjectRPCServiceAsync;
import org.bsplus.rpc.UserRPCService;
import org.bsplus.rpc.UserRPCServiceAsync;

/**
 * Utility class for looking up RPC Services
 */
public class RPCServiceLocator
{
    /**
     * Private constructor to prevent instantiation
     */
    private RPCServiceLocator()
    {
    }

    public static IssueRPCServiceAsync getIssueService()
    {
        IssueRPCServiceAsync service = (IssueRPCServiceAsync) GWT.create(IssueRPCService.class);
        configureService((ServiceDefTarget)service, "issue");
        return service;
    }

    public static ProjectRPCServiceAsync getProjectService()
    {
        ProjectRPCServiceAsync service = (ProjectRPCServiceAsync) GWT.create(ProjectRPCService.class);
        configureService((ServiceDefTarget)service, "project");
        return service;
    }

    public static UserRPCServiceAsync getUserService()
    {
        UserRPCServiceAsync service = (UserRPCServiceAsync) GWT.create(UserRPCService.class);
        configureService((ServiceDefTarget)service, "user");
        return service;
    }

    private static void configureService(ServiceDefTarget service, String relativeURL)
    {
        String moduleRelativeURL = GWT.getModuleBaseURL() + "../services/" + relativeURL;
        service.setServiceEntryPoint(moduleRelativeURL);
    }
}
